package com.bmslz.monitor.service.impl;

import com.bmslz.monitor.base.CommonConstant;
import com.bmslz.monitor.base.util.StringUtils;
import com.bmslz.monitor.base.dto.DoAlarmDto;
import com.bmslz.monitor.base.entity.AlarmModel;
import com.bmslz.monitor.base.entity.AlarmModelRule;
import com.bmslz.monitor.base.entity.ProjectAlarm;
import com.bmslz.monitor.helper.EmailHelper;
import com.bmslz.monitor.service.AlarmNotify;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Component
public class AlarmNotifyMail implements AlarmNotify {
    @Autowired
    private EmailHelper emailHelper;

    @Override
    public boolean supports(String notifyType) {
        return CommonConstant.ALARM_NOTIFY_TYPE_MAIL.equals(notifyType);
    }

    @Override
    public boolean send(DoAlarmDto doAlarmDto, AlarmModel alarmModel) {
        ProjectAlarm projectAlarm = doAlarmDto.getProjectAlarm();
        AlarmModelRule alarmModelRule = doAlarmDto.getAlarmModelRule();
        String conditionTag = alarmModelRule.getConditionTag();
        String status = projectAlarm.getStatus();//告警状态 1告警中 0解除告警
        String grade = projectAlarm.getGrade();//1 紧急 2 告警 3 预警
        String projectNo = projectAlarm.getProjectNo();
        String projectName = projectAlarm.getProjectName();
        String attKey = projectAlarm.getAttKey();
        String ruleValue = projectAlarm.getRuleValue();
        String attName = doAlarmDto.getAttName();
        String notifyData = alarmModel.getNotifyData();

        if (CommonConstant.ALARM_RULE_COMPARE_CONDITION_GT.equals(conditionTag)) {
            conditionTag = ">";
        } else if (CommonConstant.ALARM_RULE_COMPARE_CONDITION_GE.equals(conditionTag)) {
            conditionTag = ">=";
        } else if (CommonConstant.ALARM_RULE_COMPARE_CONDITION_EQ.equals(conditionTag)) {
            conditionTag = "=";
        } else if (CommonConstant.ALARM_RULE_COMPARE_CONDITION_LT.equals(conditionTag)) {
            conditionTag = "<";
        } else if (CommonConstant.ALARM_RULE_COMPARE_CONDITION_LE.equals(conditionTag)) {
            conditionTag = "<=";
        }

        if (CommonConstant.ALARM_GRADE_ERROR.equals(grade)) {
            grade = "【紧急】";
        } else if (CommonConstant.ALARM_GRADE_WARN.equals(grade)) {
            grade = "【告警】";
        } else if (CommonConstant.ALARM_GRADE_INFO.equals(grade)) {
            grade = "【预警】";
        }
        String alarmValue = projectAlarm.getAlarmValue();
        List<String> notifyToList = new ArrayList<>();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            notifyToList = objectMapper.readValue(notifyData, new TypeReference<List<String>>() {
            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        for (String sentTo : notifyToList) {
            if (StringUtils.isNotEmpty(sentTo)) {
                if (CommonConstant.STATUS_1.equals(status)) {
                    emailHelper.sendTemplateMail(sentTo, grade + "告警推送",
                            "出现等级为" + grade + "的告警事件",
                            "项目(" + projectName + "-" + projectNo + ")" + "出现等级为" + grade + "的告警",
                            "触发事件【" + attKey + "(" + attName + ")" + ":" + alarmValue + conditionTag + ruleValue + "】");
                } else if (CommonConstant.STATUS_0.equals(status)) {
                    emailHelper.sendTemplateMail(sentTo, "告警解除", "解除等级为" + grade + "的告警事件",
                            "项目(" + projectName + "-" + projectNo + ")" + "解除等级为" + grade + "的告警",
                            "触发事件【" + attKey + "(" + attName + ")" + ":" + alarmValue + conditionTag + ruleValue + "】");
                }
            }
        }
        return true;
    }
}
